package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.annotation.SystemServiceLog;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootBfMvp;
import org.footballmanager.pandafootball.system.domain.FootBfinfo;
import org.footballmanager.pandafootball.system.form.impl.FootBfMvpSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootBfMvpMapper;
import org.footballmanager.pandafootball.system.service.FootBfMvpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Service
public class FootBfMvpServiceImpl extends ServiceImpl<FootBfMvp> implements FootBfMvpService {
    public static final String SUFFIX = "BFMVPTEMP";
    @Autowired
    private FootBfMvpMapper footBfMvpMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 根据条件查询报分MVP信息列表
     *
     * @param entityname          实体类名
     * @param map                 参数
     * @param footBfMvpSearchForm 条件
     */
    @Override
    @SystemServiceLog(description = "根据条件查询报分MVP信息列表")
    public void search(String entityname, Model map, FootBfMvpSearchForm footBfMvpSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footBfMvpSearchForm);
        Page<FootBfMvp> page = PageHelper.startPage(footBfMvpSearchForm.getPageIndex(), footBfMvpSearchForm.getPageSize());
        List<FootBfMvp> list = this.findall(footBfMvpSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 根据条件删除所有报分MVP信息列表
     *
     * @param footBfMvpSearchForm 条件
     * @param request             请求
     */
    @Override
    @SystemServiceLog(description = "根据条件删除所有报分MVP信息列表")
    public void deleteAll(FootBfMvpSearchForm footBfMvpSearchForm, HttpServletRequest request) {
        if (footBfMvpSearchForm.getSelectIds() != null) {
            String[] result = footBfMvpSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 根据报分主键获取MVP信息
     *
     * @param bfId 报分主键
     * @return MVP信息
     */
    @Override
    @SystemServiceLog(description = "根据报分主键获取MVP信息")
    public FootBfMvp findByBfId(Long bfId) {
        Example example = new Example(FootBfMvp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bfId", bfId);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 根据报分主键和球员关系主键查找记录数量
     *
     * @param bfId  报分主键
     * @param fupId 球员关系主键
     * @return 记录数量
     */
    @Override
    @SystemServiceLog(description = "根据报分主键和球员关系主键查找记录数量")
    public Integer findByBfIdAndMvpId(Long bfId, Long fupId) {
        Example example = new Example(FootBfMvp.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bfId", bfId);
        criteria.andEqualTo("fupId", fupId);
        criteria.andEqualTo("delFlag", 0);
        return this.countByCondition(example);
    }

    /**
     * 保存Mvp记录到redis
     *
     * @param team    球队主键
     * @param ids     球员关系主键
     * @param request 请求
     */
    @Override
    @SystemServiceLog(description = "保存Mvp记录到redis")
    public void saveEntityTemp(Long team, Long id, HttpServletRequest request) {
        if (team != null && id != null) {
            redisService.set(SUFFIX + team, id);
        }
    }

    /**
     * 保存Mvp记录到数据库
     *
     * @param footBfinfo 报分实体类
     * @param request    请求
     */
    @Override
    @SystemServiceLog(description = "保存Mvp记录到数据库")
    public void saveEntity(FootBfinfo footBfinfo, HttpServletRequest request) {
        Integer zhuId = (Integer) redisService.get(SUFFIX + footBfinfo.getQd1());
        Integer keId = (Integer) redisService.get(SUFFIX + footBfinfo.getQd2());
        if (zhuId != null) {
            saveRecord(zhuId, footBfinfo.getId(), request);
        } else {
            saveRecord(keId, footBfinfo.getId(), request);
        }
    }

    private void saveRecord(Integer id, Long bfId, HttpServletRequest request) {
        FootBfMvp mvp = new FootBfMvp();
        mvp.setFupId(new Long(id));
        mvp.setBfId(bfId);
        this.save(mvp, request);
    }

    /**
     * 获取MVP榜
     *
     * @return MVP榜
     */
    @Override
    @SystemServiceLog(description = "获取MVP榜")
    public List<FootBfMvp> findMvpTop10() {
        return footBfMvpMapper.findMvpTop10();
    }
}
